﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Model;
using Besture.Common.Definition;
using Besture.Utils;
using System.Text.RegularExpressions;
using Besture.Common;

namespace Besture.UI.Land
{
    public partial class Land : System.Web.UI.Page
    {
        //左图
        public ImageModel _BannerLeft { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["LoginOut"]))
                {
                    Session[Constants.SESSION_KEY_LOGIN_USER] = null;
                }
                InitData();
            }
            GetBanner();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login_Click(object sender, EventArgs e)
        {
            UserModel model = new UserModel();
            UserDac dac = new UserDac();
            string checkCode = txtVailidCode.Value.Trim();
            if (checkCode.ToUpper() != Session["CheckCode"]+"")
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('验证码不正确');</script>");
                return;
            }
            string telphone = txtTelphone.Value.Trim();
            if (string.IsNullOrEmpty(telphone))
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('手机号码不能为空');</script>");
                return;
            }
            if (!Regex.IsMatch(telphone,AppConfig.SMSRegex))
            {
                this.Page.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>GetVailidCode();alert('手机号码格式不正确');</script>");
                return;
            }

            string Pwd = txtpws.Value;
            //帐号登录
            if (telphone != "" && Pwd != "")
            {
                model = dac.Login(telphone, Fun.GetMD5(Pwd));
            }
            //跳转
            if (model != null)
            {
                Session["CheckCode"] = null;
                Session[Constants.SESSION_KEY_LOGIN_USER] = model;

                Response.Redirect("/Index.aspx");
            }
            else
            {
                Fun.Alert(this.Page, "密码与用户名不匹配");
            }
        }

        #region 初始化数据
        private void InitData()
        {

        }
        #endregion

        #region 获取Banner
        /// <summary>
        /// 获取Banner
        /// </summary>
        public void GetBanner()
        {
            ImageDac imageDac = new ImageDac();
            ImageModel imgModel = imageDac.GetDataTableByCode(Constants.DICTIONARY_CODE_LandLogin, Constants.DICTIONARY_TYPE_Land);
            if (imgModel != null)
                _BannerLeft = imgModel;
            else
                _BannerLeft = new ImageModel();
        }
        #endregion
    }
}